//package io.jft.doll.tcp
//
//import io.jft.doll.tcp.event.TcpData
//import io.jft.doll.tcp.handle.BusinessHandle
//import io.jft.doll.tcp.handle.DecoderHandle
//import io.jft.doll.tcp.handle.EncoderHandle
//import com.g.base.appContent
//import com.g.base.extend.toast
//import com.g.base.help.d
//import io.netty.bootstrap.ServerBootstrap
//import io.netty.channel.Channel
//import io.netty.channel.ChannelFuture
//import io.netty.channel.ChannelInitializer
//import io.netty.channel.ChannelOption
//import io.netty.channel.nio.NioEventLoopGroup
//import io.netty.channel.socket.nio.NioServerSocketChannel
//import io.netty.handler.logging.LogLevel
//import io.netty.handler.logging.LoggingHandler
//import io.netty.handler.timeout.IdleStateHandler
//import java.util.concurrent.TimeUnit
//
//
///**
// * Created by G on 2017/12/25 0025.
// */
//val tcpServe by lazy { TcpServe() }
//
//class TcpServe {
//    private var isConnect = false
//    private var channel: Channel? = null
//    private val bootstrap: ServerBootstrap by lazy {
//        ServerBootstrap()
//                .group(NioEventLoopGroup(),NioEventLoopGroup())
//                .channel(NioServerSocketChannel::class.java)
//                .handler(LoggingHandler(LogLevel.DEBUG))
//                .childHandler(object : ChannelInitializer<Channel>() {
//                    override fun initChannel(ch: Channel?) {
//                        val pipeline = ch?.pipeline()
//                        pipeline?.addLast("IdleStateHandler", IdleStateHandler(5, 5, 20, TimeUnit.SECONDS))
//                        pipeline?.addLast("DecoderHandle", DecoderHandle())
//                        pipeline?.addLast("EncoderHandle", EncoderHandle())
//                        pipeline?.addLast("BusinessHandle", BusinessHandle())
//                    }
//                })
//                .option(ChannelOption.SO_KEEPALIVE, true)
//                .option(ChannelOption.TCP_NODELAY, true)
//    }
//
//    fun start() {
//        d("tcp server listener start")
//        if (channel != null && channel?.isActive == true) {
//            d("tcp server listener close before")
//            channel?.close()
//        }
//        val channelFuture: ChannelFuture = bootstrap
//                .bind(TcpC.TCP_SERVER, TcpC.TCP_SERVER_PORT)
//        channel = channelFuture.channel()
//        channelFuture.addListener {
//            if (it.isSuccess) {
//                d("tcp server listener succeed")
//                tcpServe.isConnect = true
//            } else {
//                d("tcp server listener error : ${it.cause()}")
//                tcpServe.isConnect = false
//                Thread.sleep(TcpC.RECONNECT_TIME)
//                start()
//            }
//        }
//    }
//
//    fun stop() {
//        d("tcp server disConnect")
//        tcpServe.isConnect = false
//        channel?.disconnect()
//                ?.addListener {
//                    if (it.isSuccess) {
//                        channel?.pipeline()?.remove("BusinessHandle")
//                    } else {
//                        channel?.close()
//                    }
//                }
//    }
//
//    fun writeAndFlush(tcpData: TcpData): ChannelFuture? {
//        if (!isConnect) {
//            appContent.toast("正在连接中 请稍后再试")
//            return null
//        }
//        return channel!!.writeAndFlush(tcpData)
//    }
//}
//
